/**
 *====================================================
 * 文件名称: LogisticsCompanyServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.member.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.BooleanUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.exception.NotFoundException;
import com.yuexunit.finance.member.entity.LogisticsCompanyEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @ClassName: LogisticsCompanyServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:55:49
 */

@Implements(LogisticsCompanyService.class)
public class LogisticsCompanyServiceImpl implements LogisticsCompanyService {

	@Override
	@Transactional
	public Long createLogisticsCompany(LogisticsCompanyEntity logisticsCompanyEntity) {
		logisticsCompanyEntity.setEnabled(true);
		logisticsCompanyEntity.setCreateDateTime(DateUtil.now());
		logisticsCompanyEntity.setUpdateDateTime(DateUtil.now());

		PersistUtil.insert(logisticsCompanyEntity);
		return logisticsCompanyEntity.getCompanyId();
	}

	@Override
	@Transactional
	public void updateLogisticsCompany(LogisticsCompanyEntity logisticsCompanyEntity) {
		LogisticsCompanyEntity entity = inquireLogisticsCompanyDetail(logisticsCompanyEntity.getCompanyId());
		entity.setCompanyName(logisticsCompanyEntity.getCompanyName());
		entity.setContactor(logisticsCompanyEntity.getContactor());
		entity.setContactorTel(logisticsCompanyEntity.getContactorTel());
		entity.setRemark(logisticsCompanyEntity.getRemark());
		entity.setReportedState(logisticsCompanyEntity.getReportedState());
		entity.setCompanyAccount(logisticsCompanyEntity.getCompanyAccount());
		entity.setUpdateDateTime(DateUtil.now());
		entity.setEffective(logisticsCompanyEntity.getEffective());
		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void disableLogisticsCompany(Long logisticsCompanyId) {
		LogisticsCompanyEntity entity = inquireLogisticsCompanyDetail(logisticsCompanyId);
		entity.setEnabled(false);
		entity.setUpdateDateTime(DateUtil.now());

		PersistUtil.update(entity);
	}

	@Override
	public LogisticsCompanyEntity inquireLogisticsCompanyDetail(Long logisticsCompanyId) {
		LogisticsCompanyEntity entity = PersistUtil.get(LogisticsCompanyEntity.class, logisticsCompanyId);
		if (entity == null) {
			throw new NotFoundException("该物流公司不存在");
		}
		return entity;
	}

	public LogisticsCompanyEntity inquireTenantLogisticsCompany(Long tenantId) {
		DetachedCriteria dc = DetachedCriteria.forClass(LogisticsCompanyEntity.class);
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		dc.add(Restrictions.eq("tenantId", tenantId));
		List<LogisticsCompanyEntity> list = PersistUtil.get(dc);
		if (list.size() <= 0) {
//			throw new NotFoundException("该物流公司不存在！");
			return null;
		} else if (list.size() > 1) {
			throw new NotFoundException("该帐号存在多个物流公司中！");
		}
		return list.get(0);
	}
	
	public LogisticsCompanyEntity inquireTenantLogisticsCompanyByAccountId(Long accountId) {
		DetachedCriteria dc = DetachedCriteria.forClass(LogisticsCompanyEntity.class);
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		dc.add(Restrictions.eq("accountId", accountId));
		List<LogisticsCompanyEntity> list = PersistUtil.get(dc);
		if (list.size() <= 0) {
			throw new NotFoundException("该物流公司不存在！");
		} else if (list.size() > 1) {
			throw new NotFoundException("该帐号存在多个物流公司中！");
		}
		return list.get(0);
	}
	
	@Override
	public LogisticsCompanyEntity inquireLogisticsCompanyByCompanyName(String companyName) {
		DetachedCriteria dc = DetachedCriteria.forClass(LogisticsCompanyEntity.class);
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		dc.add(Restrictions.eq("companyName", companyName));
		List<LogisticsCompanyEntity> list = PersistUtil.get(dc);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			throw new NotFoundException("该物流公司不存在！");
		}
	}

	@Override
	public PageList<LogisticsCompanyEntity> inquireLogisticsCompanyPageList(Date createDateTimeBegin, Date createDateTimeEnd, String companyName, String companyAccount, Integer reportState, Integer effective, PageCriteria page) {
		String value = "";
		DetachedCriteria criteria = DetachedCriteria.forClass(LogisticsCompanyEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));

		if (createDateTimeBegin != null) {
			criteria.add(Restrictions.ge("createDateTime", createDateTimeBegin));
		}
		if (createDateTimeEnd != null) {
			criteria.add(Restrictions.le("createDateTime", createDateTimeEnd));
		}
		if (StringUtil.isNotBlank(companyName)) {
			value = "%" + companyName + "%";
			criteria.add(Restrictions.like("companyName", value));
		}

		if (StringUtil.isNotBlank(companyAccount)) {
			value = "%" + companyAccount + "%";
			criteria.add(Restrictions.like("companyAccount", value));
		}
		if (reportState != null) {
			criteria.add(Restrictions.eq("reportedState", BooleanUtils.toBoolean(reportState)));
		}
		if (effective != null) {
			criteria.add(Restrictions.eq("effective", BooleanUtils.toBoolean(effective)));
		}
		PageList<LogisticsCompanyEntity> logisticsCompanyList = PersistUtil.get(criteria, page);
		return logisticsCompanyList;
	}

	@Override
	public PageList<LogisticsCompanyEntity> inquireLogisticsCompanyListByCompanyName(
			String companyName, PageCriteria page) {
		String value = "";
		DetachedCriteria criteria = DetachedCriteria.forClass(LogisticsCompanyEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (StringUtil.isNotBlank(companyName)) {
			value = "%" + companyName + "%";
			criteria.add(Restrictions.like("companyName", value));
		}
		criteria.add(Restrictions.eq("effective", Boolean.TRUE));
		
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<LogisticsCompanyEntity> inquireLogisticsCompanyList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(LogisticsCompanyEntity.class);
		return PersistUtil.get(criteria);
	}
}
